package com.abigdreamer.im.server.ui.desktop.internal;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;

import com.abigdreamer.im.server.tools.ServerUtils;
import com.abigdreamer.im.server.ui.desktop.DeskTopConfig;
import com.abigdreamer.im.server.ui.desktop.EIMDesktop;
import com.abigdreamer.im.server.ui.resourse.ResourseMannager;


/**
 * 
 * 
 */
public class DeskPropertyPanel extends javax.swing.JPanel {
	EIMDesktop desktop=null;
	String imageName=null;
    public DeskPropertyPanel() {
    	desktop=EIMDesktop.getInstance();
        initComponents();
    }
    private void initComponents() {
        jComboBox1 = new javax.swing.JComboBox();
        sureButton = new javax.swing.JButton();
        resetButton = new javax.swing.JButton();
        useButton = new javax.swing.JButton();
        imageLabel = new javax.swing.JLabel();//水平:279 垂直:194
      
        if(imageName==null){
        	imageName=desktop.getDeskTopConfig().getWallPaper();
        	if(imageName==null){
        	imageName="wallimage/Bliss.jpg";
        	}
        }
        ImageIcon icon=ResourseMannager.getImageIcon(imageName, 279, 194);
        imageLabel.setIcon(icon);
        String[] iamgeNames=ResourseMannager.getWallImageNames();
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(iamgeNames));
        jComboBox1.addItemListener(new MyItemListener());

        sureButton.setText("预览");
        sureButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                sureButtonMouseClicked(evt);
            }
        });

        resetButton.setText("\u53d6\u6d88");
        resetButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                resetButtonMouseClicked(evt);
            }
        });

        useButton.setText("\u5e94\u7528");
        useButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                useButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(sureButton)
                                .addGap(50, 50, 50)
                                .addComponent(resetButton)
                                .addGap(18, 18, 18)
                                .addComponent(useButton))
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(imageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(64, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(imageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(resetButton)
                    .addComponent(useButton)
                    .addComponent(sureButton))
                .addGap(32, 32, 32))
        );
    }

    private void useButtonMouseClicked(java.awt.event.MouseEvent evt) {
    	String newName="wallimage/"+(String)(jComboBox1.getSelectedItem());
    	PropertyFrame.getInstance().setVisible(false);
    	EIMDesktop desktop=EIMDesktop.getInstance();
    	desktop.intWallPaper(newName);
    	DeskTopConfig deskTopConfig =desktop.getDeskTopConfig();
    	deskTopConfig.setWallPaper(newName);
    	ServerUtils.writeDeskTopConfig(deskTopConfig);
    	imageName=newName;
    	System.out.println(newName);
    	
    }

    private void resetButtonMouseClicked(java.awt.event.MouseEvent evt) {
    	desktop.intWallPaper(imageName);
    	desktop.repaint();
    	PropertyFrame.getInstance().setVisible(false);
    }

    private void sureButtonMouseClicked(java.awt.event.MouseEvent evt) {
      	String newName=(String)(jComboBox1.getSelectedItem());
    	EIMDesktop desktop=EIMDesktop.getInstance();
    	desktop.intWallPaper("wallimage/"+newName);
    	desktop.repaint();
    	System.out.println(newName);
    }
    
    class MyItemListener implements ItemListener {
    	public void itemStateChanged(ItemEvent e) {
    	   if (e.getStateChange() == ItemEvent.SELECTED) {
    	    JComboBox jcb = (JComboBox) e.getSource();
    	    String newName=(String)(jcb.getSelectedItem());
    	    ImageIcon icon=ResourseMannager.getImageIcon("wallimage/"+newName, 279, 194);
            imageLabel.setIcon(icon);
    	   } else {
    	   }
    	}
    	}

    
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    private javax.swing.JLabel imageLabel;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JButton resetButton;
    private javax.swing.JButton sureButton;
    private javax.swing.JButton useButton;
    // 变量声明结束//GEN-END:variables
    
}